package nuist.hanniko.stu.webbackend.service.impls;

import io.jsonwebtoken.Claims;
import nuist.hanniko.stu.webbackend.dao.mapper.UserMapper;
import nuist.hanniko.stu.webbackend.dao.mapper.UserRoleMapper;
import nuist.hanniko.stu.webbackend.dao.redis.TokenDAO;
import nuist.hanniko.stu.webbackend.model.User;
import nuist.hanniko.stu.webbackend.service.UserService;
import nuist.hanniko.stu.webbackend.util.TokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private TokenDAO tokenDAO;

    @Override
    public int register(User user) {
        if (!isUserExist(user.getUid())) {
            userMapper.add(user);
            return SUCCESS;
        }
        return FAILURE;
    }

    @Override
    public Map<String, Object> login(String uid, String password) {
        Map<String, Object> map = null;

        User user = userMapper.selectUserById(uid);
        if (user != null && password.equals(user.getPassword())) {
            map = new HashMap<>();
            List<String> roles = userRoleMapper.getAllRolesById(uid);
            map.put("roles", roles);
            map.put("token", getToken(uid));
        }

        return map;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map<String, Object> login(String token) {
        Map<String, Object> map = null;
        Claims claims = TokenHelper.extractJwt(token);

        if (claims != null) {
            map = new HashMap<>();
            List<String> roles = (List) claims.get("role");
            map.put("roles", roles);
            map.put("token", token);
        }

        return map;
    }

    @Override
    public int modify(User user) {
        if (isUserExist(user.getUid())) {
            userMapper.updateUserById(user);
            return SUCCESS;
        }
        return FAILURE;
    }

    @Override
    public int addRole(String uid, String role) {
        if (isUserExist(uid) && !isUserRoleExist(uid, role)) {
            userRoleMapper.add(uid, role);
            return SUCCESS;
        }
        return FAILURE;
    }

    @Override
    public int deleteRole(String uid, String role) {
        if (isUserExist(uid) && isUserRoleExist(uid, role)) {
            userRoleMapper.delete(uid, role);
            return SUCCESS;
        }
        return FAILURE;
    }

    public boolean isUserExist(String uid) {
        return !(userMapper.selectUserById(uid) == null);
    }

    public boolean isUserRoleExist(String uid, String role) {
        return !(userRoleMapper.getRoleByIdAndRole(uid, role) == null);
    }

    private String getToken(String uid) {
        String token = (String) tokenDAO.getToken(uid);
        List<String> roles = userRoleMapper.getAllRolesById(uid);
        if (token == null || TokenHelper.extractJwt(token) == null) {
            token = TokenHelper.generateToken(uid,
                    roles.toArray(new String[roles.size()]));
            tokenDAO.createToken(uid, token);
        }
        return token;
    }

    private String getToken(String uid, List<String> roles) {
        String token = (String) tokenDAO.getToken(uid);
        if (token == null) {
            token = TokenHelper.generateToken(uid,
                    roles.toArray(new String[roles.size()]));
            tokenDAO.createToken(uid, token);
        }
        return token;
    }
}
